﻿using MediatR;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using xiaodingmarket_Domain.Commands.Order;
using xiaodingmarket_Domain.Events.Order;
using xiaodingmarket_Domain.Interfaces;
using xiaodingmarket_Domain.Interfaces.IRepository;
using xiaodingmarket_Domain.Models;
using xiaodingmarket_Domain_core.Bus;
using xiaodingmarket_Domain_core.Notificatiions;
using static xiaodingmarket_Domain.Models.EunmType;

namespace xiaodingmarket_Domain.CommandHandlers
{
    public class OrderCommandHandler : CommandHandler,
        IRequestHandler<CreateNewOrderCommand, bool>,
        IRequestHandler<UpdateOrderCommand, bool>,
        IRequestHandler<RemoveOrderCommand, bool>
    {
        private readonly IOrderRepository _orderRepository;
        private readonly IMediatorHandler Bus;

        public OrderCommandHandler(IOrderRepository orderRepository,
                                      IUnitOfWork uow,
                                      IMediatorHandler bus,
                                      INotificationHandler<DomainNotification> notifications) : base(uow, bus, notifications)
        {
            _orderRepository = orderRepository;
            Bus = bus;
        }
        /// <summary>
        /// 创建订单，这里同时要去修改一下商品的状态
        /// </summary>
        /// <param name="message"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public Task<bool> Handle(CreateNewOrderCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return Task.FromResult(false);
            }
            var orderDetail = new OrderDetail(Guid.NewGuid(),message.Productid, message.price);
            var order = new OrderCommodity(Guid.NewGuid(), orderDetail, message.Buyer, message.Seller, OrderCondition.OnDeal, DateTime.Now, DateTime.Now, message.Seller, message.Seller, message.Remark, "", false);
            _orderRepository.Add(order);

            if (Commit())
            {
               // Bus.RaiseEvent(new OrderCreatEvent(message.Seller,message.LogTime,message.Productid,message.Buyer));
            }

            return Task.FromResult(true);

        }

        public Task<bool> Handle(UpdateOrderCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return Task.FromResult(false);
            }
            var order = _orderRepository.GetById(message.Id);
            order.Condition = message.Condition;
            order.ModifyTime = message.ModifyTime;
            order.ModifyUserId = message.ModifyUserId;
            order.Remark = message.Remark;
            _orderRepository.Update(order);
            if (Commit())
            {
               // Bus.RaiseEvent(new OrderUpdateEvent(message.Id,message.Condition,message.ModifyUserId,message.ModifyTime,message.Remark));
            }
            return Task.FromResult(true);
            
        }

        public Task<bool> Handle(RemoveOrderCommand message, CancellationToken cancellationToken)
        {
            if (!message.IsValid())
            {
                NotifyValidationErrors(message);
                return Task.FromResult(false);
            }
            //逻辑删除
            var com = _orderRepository.GetById(message.Id);
            com.IsDelete = true;

            if (Commit())
            {
               // Bus.RaiseEvent(new OrderRemoveEvent(message.Id));
            }

            return Task.FromResult(true); throw new NotImplementedException();
        }
    }
}
